package com.telecomyt.jwportal.utils;

import android.util.Log;

import java.text.SimpleDateFormat;

/**
 * @author created by chengDa
 * @time 2020/9/17  9:50
 * @description
 */
public class DateUtils {
    /**
     * 秒级别时间戳 对比
     */
    public static String getStandardDate(String timeStr) {
        String s = dateTimeStamp(timeStr, "yyyy-MM-dd HH:mm:ss");
        String temp = "";
        try {
            long now = System.currentTimeMillis() / 1000;
            long time = Long.parseLong(s);
            Log.i("time", time + "");
            long diff = now - time;
            long year = diff / (60 * 60 * 24 * 30 * 12);
            long months = diff / (60 * 60 * 24 * 30);
            long week = diff / (60 * 60 * 24 * 7);
            long days = diff / (60 * 60 * 24);
            long hours = (diff - days * (60 * 60 * 24)) / (60 * 60);
            long minutes = (diff - days * (60 * 60 * 24) - hours * (60 * 60)) / 60;
            if (year > 0) {
                temp = year + "年前";
            } else if (months > 0) {
                temp = months + "月前";
            } else if (week > 0) {
                temp = week + "周前";
            } else if (days > 0) {
                temp = days + "天前";
            } else if (hours > 0) {
                temp = hours + "小时前";
            } else if (minutes > 0) {
                temp = minutes + "分钟前";
            } else {
                temp = diff + "秒前";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return temp;
    }


    /**
     * * 日期格式字符串转换成时间戳
     * * @param date_str 字符串日期
     */
    public static String dateTimeStamp(String date_str, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return String.valueOf(sdf.parse(date_str).getTime() / 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 秒级别时间戳 对比 long类型
     */
    public static String getStandardDate(long timeStr) {
        String temp = "";
        try {
            long now = System.currentTimeMillis() / 1000;
            long diff = now - timeStr;
            long year = diff / (60 * 60 * 24 * 30 * 12);
            long months = diff / (60 * 60 * 24 * 30);
            long week = diff / (60 * 60 * 24 * 7);
            long days = diff / (60 * 60 * 24);
            long hours = (diff - days * (60 * 60 * 24)) / (60 * 60);
            long minutes = (diff - days * (60 * 60 * 24) - hours * (60 * 60)) / 60;
            if (year > 0) {
                temp = year + "年前";
            } else if (months > 0) {
                temp = months + "月前";
            } else if (week > 0) {
                temp = week + "周前";
            } else if (days > 0) {
                temp = days + "天前";
            } else if (hours > 0) {
                temp = hours + "小时前";
            } else if (minutes > 0) {
                temp = minutes + "分钟前";
            } else {
                temp = diff + "秒前";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return temp;
    }
}
